package com.viphuli.adapter;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.DateUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.http.bean.part.AccountUser;
import com.viphuli.http.bean.part.PurchasePayComment;
import com.viphuli.util.ImageUtils;

public class ServiceInfoListViewAdapter extends BaseAdapter {
	private Activity act;
	private List<PurchasePayComment> list;

	public ServiceInfoListViewAdapter(Activity act, List<PurchasePayComment> list) {
		this.act = act;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(parent.getContext(), R.layout.listview_home_service_info_comment_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		//填充数据
		final PurchasePayComment item = list.get(position);

		ImageUtils.load(holder.icon, item.getUser().getPortrait());

		AccountUser user = item.getUser();
		if (user.getUserName().equals(user.getTel())) {//当用户名为手机号时(显示手机号并加星)
			holder.name.setText(item.getUser().telSecurity());
		} else {
			holder.name.setText(item.getUser().getUserName());
		}

		holder.star.setRating(item.getScoreTotal());
		holder.content.setText(item.getContent());
		holder.time.setText(DateUtils.formatDay(item.getTime()));

		return convertView;
	}

	protected class ViewHolder {

		@Bind(R.id.id_main_home_service_item_info_comment_icon)
		ImageView icon;
		@Bind(R.id.id_main_home_service_item_info_comment_user_name)
		TextView name;
		@Bind(R.id.id_main_home_service_item_info_comment_star)
		RatingBar star;
		@Bind(R.id.id_main_home_service_item_info_comment_content)
		TextView content;
		@Bind(R.id.id_main_home_service_item_info_comment_time)
		TextView time;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
